#ifndef _IMAGE_EXTRACTOR_
#define _IMAGE_EXTRACTOR_

#include <stdio.h>

typedef struct tagIndex{
	int id;
	int name_offset;
	int res_offset;
} Index;

void get_index_block(FILE *src, Index *index_array[], int count);
int get_valid_length(FILE *src);
void sort_index_block_by_id(Index *index_array[], int count);
void sort_index_block_by_res_offset(Index *index_array[], int count);
void offset_index_block_res_offset(Index *index_array[], int count, int start, int offset);
void write_index_block(Index *index_array[], int count, FILE *out);
/**
 * @brief return the start position of the specified picture.
 * 
 * @param src the file input stream pointer.
 * @param sn the specified serial number base 1.
 *
 * @return the start position of the specified picture.
 */
long get_start_position(FILE *src, unsigned int sn);

/**
 * @brief return the end position of the specified picture. 
 * 
 * @param src the file input stream pointer.
 * @param src sn the specified serial number base 1.
 *
 * @return the end position of the specified picture.
 */
long get_end_position(FILE *src, unsigned int sn);

/**
 * @brief get the start and end positions of the specified pricture 
 * 
 * @param src the file input stream pointer.
 * @param sn the specified serial number base 1.
 * @param start_pos the start position of the specified picture which will be got.
 * @param end_pos the end position of the specified picture which will be got.
 */
void get_position(FILE *src, unsigned int sn, long *start_pos, long *end_pos);

/**
 * @brief is the block match the start tag of png file? 
 * 
 * @param block the input block.
 *
 * @return 0 for true and non-0 for false.
 */
int is_png_start(const char *block);

/**
 * @brief is the block match the end tag of png file?
 *
 * @param block the input block.
 *
 * @param 0 for true and non-0 for false.
 */
int is_png_end(const char *block);

/**
 * @brief extract the specified pictrue in rcc file to disk.
 *
 * @param src the input stream pointer.
 * @param sn the specified serial number.
 * @param out output stream pointer.
 *
 * @return -1 for failed and other for successed.
 */
int extract_file(FILE *src, unsigned int sn, FILE *out);
int extract_all_file(FILE *src, const char *out_dir);

/**
 * @brief get the count of pictures in the rcc file.
 * 
 * @param src the input stream pointer.
 * 
 * @return the count of pictures in the specified rcc file.
 */
int get_picture_count(FILE *src);
/**
 * @brief replace the specified pictrue with another one.
 *
 * @param src the *.rcc file will be modified.
 * @param sn the specified serial number.
 * @param new_picture the new picture file stream pointer.
 * @param out the output file.
 *
 * @return 0 upon success, nonzero value otherwise.
 */
int replace(FILE *src, int sn, FILE *new_picture, FILE *out);

/**
 * @brief replace the head info when replace picture.
 *
 * @param src the *.rcc file will be modified.
 * @param sn the specified serial number.
 * @param new_picture the new picture file stream pointer.
 * @param out the output file.
 *
 * @return 0 upon success, nonzero value otherwise.
 */
int _replace_head(FILE *src, int sn, FILE *new_picture, FILE *out);

/**
 * @brief get the specified picture's size(in byte)
 *
 * @param src the rcc file will be modified.
 * @param sn the specified serial number.
 *
 * @return the size of the specified picture.
 */
int get_picture_size(FILE *src, unsigned int sn);
#endif
